﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
/*
 * Coder:Tieria
 * WriteTime:2015.2.26
 * FileName:PathHelper.cs
 * ReMark:路径帮助类
 */
namespace FileDFS.Utils
{
    public class PathHelper
    {
        /// <summary>
        /// If the path is absolute is return as is, otherwise is combined with AppDomain.CurrentDomain.SetupInformation.ApplicationBase
        /// The path are always server relative path.
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns>字符串（本地路径）</returns>
        public static string LocateServerPath(string path)
        {
            if (System.IO.Path.IsPathRooted(path) == false)
                path = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, path);

            return path;
        }


        /// <summary>
        /// 合并相对Url和参照Url
        /// </summary>
        /// <param name="baseUrl">参照Url</param>
        /// <param name="relativeUrl">相对Url</param>
        /// <returns>字符串（Url）</returns>
        public static string CombineUrl(string baseUrl, string relativeUrl)
        {
            if (relativeUrl.Length == 0 || relativeUrl[0] != '/')
                relativeUrl = '/' + relativeUrl;

            if (baseUrl.Length > 0 && baseUrl[baseUrl.Length - 1] == '/')
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);

            return baseUrl + relativeUrl;
        }
        public static string CombineRelativePath(params string[] paths)
        {
            string str = "";
            for (int i = 0; i < paths.Length; i++)
            {
                str = CombineRelativePath(str, paths[i]);
            }
            return str;
        }
        //相对路径转换成绝对路径
        public static string RelativeToAbsolute(string path)
        {
            path = path.Replace(@"/", @"\");
            return path;
        }
        //合并盘符+绝对路径
        public static string CombineDriverAbsolute(string driver, string absolutePath)
        {
            if (driver.Length > 0 && driver.IndexOf(":") < 0)
            {
                driver = driver + @":";
            }
            driver += @"\";
            if (absolutePath.Length > 0)
            {
                if (absolutePath.StartsWith(@"\"))
                {
                    absolutePath = absolutePath.Substring(1);
                }
                if (absolutePath[absolutePath.Length - 1] != '\\')
                {
                    absolutePath = absolutePath + @"\";
                }

            }
            return System.IO.Path.Combine(driver, absolutePath);
        }
        //合并两个路径  比如路径 User,2015 合并后为 /User/2015
        public static string CombineRelativePath(string basePath, string relativePath)
        {
            if (basePath.Length > 0)
            {
                if (basePath[0] != '/')
                {
                    basePath = @"/" + basePath;
                }
                if (basePath[basePath.Length - 1] == '/')
                {
                    basePath = basePath.Substring(0, basePath.Length - 1);
                }
            }
            if (relativePath.Length > 0 && relativePath[0] != '/')
            {
                relativePath = '/' + relativePath;
            }
            return basePath + relativePath;
        }
        /// <summary>
        /// 根据全路径获取扩展名
        /// </summary>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        public static string GetExtensionByFullPath(string fullPath)
        {
            string extension = "";
            int point=fullPath.LastIndexOf('.')+1;
            if (point > 0)
            {
                extension = fullPath.Substring(point);
            }
            return extension;
        }


        /// <summary>
        /// Get the web site application root path. Combine the request.Url.GetLeftPart(UriPartial.Authority) with request.ApplicationPath
        /// </summary>
        /// <returns>字符串（Url）</returns>
        public static string GetWebAppUrl()
        {
            HttpRequest request = HttpContext.Current.Request;

            return CombineUrl(request.Url.GetLeftPart(UriPartial.Authority), request.ApplicationPath);
        }
    }
}
